//
//  YMBusSectionHeaderView.m
//  GroupFoldDemo
//
//  Created by lantaiyuan on 16/9/20.
//  Copyright © 2016年 youmy. All rights reserved.
//

#import "YMBusSectionHeaderView.h"
#import "UIView+Extension.h"
#import "NSString+Extension.h"
#import "YMSectionModel.h"
#import "UIColor+Extension.h"

@interface YMBusSectionHeaderView ()
@property (weak, nonatomic) IBOutlet UIView *vehicleView;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *stationLabel;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeStationLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *timeWidthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *distanceWidthContraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *timeDistanceWidthConstraint;
@end

@implementation YMBusSectionHeaderView

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [_stationLabel drawBorderWithColor:[UIColor orangeColor] radius:3.0];
    
    _timeWidthConstraint.constant = [NSString boundingRectWithString:_timeLabel.text font:_timeLabel.font].width + 2.5;
    _distanceWidthContraint.constant = [NSString boundingRectWithString:_distanceLabel.text font:_distanceLabel.font].width + 2.5;
    
    _timeDistanceWidthConstraint.constant = _timeWidthConstraint.constant + 10 + _distanceWidthContraint.constant;
    
    _vehicleView.backgroundColor = [UIColor whiteColor];
    [_vehicleView drawBorderWithColor:[UIColor colorWithHexString:@"#dddddd"] radius:3.0];
    
     [_vehicleView clipsToBounds];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
        self.frame = frame;
    }
    return self;
}

- (void)setSectionModel:(YMSectionModel *)sectionModel{
    _sectionModel = sectionModel;
    _nameLabel.text = sectionModel.name;
    _timeStationLabel.text = [NSString stringWithFormat:@"%zd站 %@",sectionModel.stations.count, sectionModel.time];
    if (sectionModel.isExpanded) {
        _flagImageView.image = [UIImage imageNamed:@"packup"];
    }else{
        _flagImageView.image = [UIImage imageNamed:@"an"];
    }
}

- (IBAction)onExpand:(UIButton *)sender {
//    sender.selected = !sender.selected;
//    
//    if (sender.selected) {
//        _flagImageView.image = [UIImage imageNamed:@"packup"];
//    }else{
//        _flagImageView.image = [UIImage imageNamed:@"an"];
//    }
    
    self.sectionModel.isExpanded = !self.sectionModel.isExpanded;
    
    if (self.expandCallBack) {
        self.expandCallBack(self.sectionModel.isExpanded);
    }
}

- (IBAction)lookDetail:(UIButton *)sender {
    if (_lookDetailBlock) {
        _lookDetailBlock();
    }
}

@end
